//
//  AllocateCourseViewController.m
//  TraineeManagementSystem
//
//  Created by Dhanashree Bodas on 29/01/15.
//  Copyright (c) 2015 Dhanashree Bodas. All rights reserved.
//

#import "AllocateCourseViewController.h"

@interface AllocateCourseViewController ()
{
    BOOL isSearching;
    NSMutableArray *filteredList;
    NSMutableArray *selectedRows;
    
}
@end

@implementation AllocateCourseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    isSearching=NO;
    filteredList=[[NSMutableArray alloc]init];
    selectedRows=[[NSMutableArray alloc]init];
    [self loadData];

    
    NSDateFormatter *dateformate=[[NSDateFormatter alloc]init];
    
    [dateformate setDateFormat:@"dd/MM/YYYY"];
    self.allocationDate.text=[dateformate stringFromDate:[NSDate date]];
    
    // Do any additional setup after loading the view.
}

-(void)loadData{
    NSArray *arr;
    TraineeCourseTable *tc=[[TraineeCourseTable alloc]init];
    arr=[tc listUnallocatedCourse:_tid];
    if(arr.count >0)
    {
        _list=[[NSMutableArray alloc ]initWithArray:arr];
    }
    else
    {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Allocate courses" message:@"No new course to add.All Courses are allocated......!!!!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
        _allocateButton.enabled=NO;

        
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}






 //search---------------
- (void)filterListForSearchText:(NSString *)searchText
{
    [filteredList removeAllObjects];     for (Course *c1 in _list) {
        NSString *title=c1.cname;
        NSRange nameRange = [title rangeOfString:searchText options:NSCaseInsensitiveSearch];
        if (nameRange.location != NSNotFound) {
            [filteredList addObject:title];
        }
    }
}


- (void)searchDisplayControllerWillBeginSearch:(UISearchController *)controller {
    
    isSearching = YES;
}


- (void)searchDisplayControllerWillEndSearch:(UISearchController *)controller {
    
    isSearching = NO;
}


- (BOOL)searchDisplayController:(UISearchController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterListForSearchText:searchString];
    
    return YES;
}

- (BOOL)searchDisplayController:(UISearchController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
    [self filterListForSearchText:[self.searchDisplayController.searchBar text]];
    return YES;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (isSearching) {
        
        return [filteredList count];
    } else {
        
        return [_list count];
    }
}

-(UITableViewCell *)tableView:(UITableView *)atableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier=@"Cell";
    UITableViewCell *cell = [atableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    
    
    NSString *title;
    if (isSearching && [filteredList count]) {
        
        title = [filteredList objectAtIndex:indexPath.row];
    }
    else
    {
        Course *c1 = [_list objectAtIndex:indexPath.row ];
        title=c1.cname;
    }
    
    cell.textLabel.text = title;
    
    return cell;
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
    
    
    if (thisCell.accessoryType == UITableViewCellAccessoryNone)
    {
        thisCell.accessoryType = UITableViewCellAccessoryCheckmark;
       
        for(int i=0;i<_list.count;i++)
        {
             Course *c1 = [_list objectAtIndex:i];
            if([thisCell.textLabel.text isEqualToString:c1.cname])
            {
             
                [selectedRows addObject:c1];
            }
        }
        
    }
    else{
        thisCell.accessoryType = UITableViewCellAccessoryNone;
      
        for(int i=0;i<_list.count;i++)
        {
            Course *c1 = [_list objectAtIndex:i];
            if([thisCell.textLabel.text isEqualToString:c1.cname])
            {
            
                [selectedRows removeObject:c1];
            }
        
        }
    }
}


- (IBAction)allocationPressed:(id)sender
{
//    for(int i=0;i<selectedRows.count;i++)
//    {
//        NSLog(@"selected row=%@",[selectedRows objectAtIndex:i]);
//    }

   
    TraineeCourseTable *tc=[[TraineeCourseTable alloc] init];
    if(selectedRows.count==0)
    {
    
        
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Validation" message:@"Select the Courses first" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
        
        [alert show];
        
    }
    else
    {
    int chk=[tc allocateCourse:_tid courseName:selectedRows andDate:self.allocationDate.text];
        NSLog(@"%d",chk);
    if(chk > 0)
    {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Allocate courses" message:@"Courses aalocated sucessfully" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
        [self dismissViewControllerAnimated:YES completion:nil];
    
    }
  }
    
}


- (IBAction)cancelPressed:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
